<form id="<?php echo $uniqid;?>" method="post" style="padding: 8px;">
    <table border="0" width="100%" cellspacing="1" cellpadding="5" class="formtable">
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">等级名称：</td>
            <td width="80%"><input type="text" style="width:400px;" v-model="params.name"
                                   class="forminput inputstyle textbox-text validatebox-text textbox-prompt easyui-validatebox"
                                   placeholder="请输入等级名称.." required="true"></td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">等级徽章：</td>
            <td class="main">
                <div title="上传图片" style="display: flex;">
                    <div class="img-box" v-if="params.image">
                        <a class="linkWrapper" target="_blank" :href="params.image" title="查看原图">
                            <img height="70px" :src="params.image+'?imageView2/1/w/100/h/100'"></a>
                        <div class="img-box-del" @click="params.image=''">
                            <img class="label-auto" src="/static/Easyui/themes/2019/icons/del_tr.gif">
                        </div>
                    </div>
                    <div class="add-image-button" v-else>
                        <a class="btn btn-default btn-lg" @click="uploadBox('agentlevelVm.params.image')"
                           href="javascript:void(0);">
                            <i class="iconfont icon-tianjia"></i>
                        </a>
                    </div>
                </div>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">直升产品：</td>
            <td width="80%"><textarea style="width:400px; height: 78px; text-indent: 0;padding-left: 5px; padding-right: 5px;" v-model="params.upgrade_goods"
                                   class="forminput inputstyle textbox-text validatebox-text textbox-prompt easyui-validatebox"
                                      placeholder="请输入直升产品.." required="true"></textarea>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">默认级差分红比例：</td>
            <td width="80%">
                <div class="display-flex" style="width: 320px; height: 40px;">
                    <el-input v-model="params.commission_rules" size="small" type="number" width="200px" v-enter-number>
                        <template slot="append">%</template>
                    </el-input>
                </div>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">默认平级一分红：</td>
            <td width="80%">
                <div class="display-flex" style="width: 350px; height: 40px;">
                    <div style="width: 160px;">
                        <el-input v-model="params.award_ping1.ratio" size="mini" v-enter-number style="width: 160px"
                                  :disabled="!!params.award_ping1.money" @input="inputChange">
                            <template slot="append">%</template>
                        </el-input>
                    </div>
                    <div style="width: 160px;margin-left: 20px">
                        <el-input v-model="params.award_ping1.money" size="mini" v-enter-number style="width:160px"
                                  :disabled="!!params.award_ping1.ratio" @input="inputChange">
                            <template slot="append">元</template>
                        </el-input>
                    </div>
                </div>

        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">默认平级二分红：</td>
            <td width="80%">
                <div class="display-flex" style="width: 350px; height: 40px;">
                    <div style="width: 160px;">
                        <el-input v-model="params.award_ping2.ratio" size="mini" v-enter-number style="width: 160px"
                                  :disabled="!!params.award_ping2.money" @input="inputChange">
                            <template slot="append">%</template>
                        </el-input>
                    </div>
                    <div style="width: 160px;margin-left: 20px">
                        <el-input v-model="params.award_ping2.money" size="mini" v-enter-number style="width:160px"
                                  :disabled="!!params.award_ping2.ratio" @input="inputChange">
                            <template slot="append">元</template>
                        </el-input>
                    </div>
                </div>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td class="tlabel">默认平级三分红：</td>
            <td width="80%">
                <div class="display-flex" style="width: 350px; height: 40px;">
                    <div style="width: 160px;">
                        <el-input v-model="params.award_ping3.ratio" size="mini" v-enter-number style="width: 160px"
                                  :disabled="!!params.award_ping3.money" @input="inputChange">
                            <template slot="append">%</template>
                        </el-input>
                    </div>
                    <div style="width: 160px;margin-left: 20px">
                        <el-input v-model="params.award_ping3.money" size="mini" v-enter-number style="width:160px"
                                  :disabled="!!params.award_ping3.ratio" @input="inputChange">
                            <template slot="append">元</template>
                        </el-input>
                    </div>
                </div>
            </td>
        </tr>
    </table>

    <table border="0" width="100%" cellspacing="1" cellpadding="5" class="formtable">
        <tr bgcolor="#FFFFFF">
            <td><b>升级规则</b></td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_consume.flag">
                    <el-checkbox true-label="1" false-label="0">团队业绩金额满
                        <el-input v-model="team_consume.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">元</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="total_consume.flag">
                    <el-checkbox true-label="1" false-label="0">自购订单金额满
                        <el-input v-model="total_consume.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">元</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child.flag">
                    <el-checkbox true-label="1" false-label="0">团队人数满
                        <el-input v-model="team_child.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child_direct.flag">
                    <el-checkbox true-label="1" false-label="0">直推客户满
                        <el-input v-model="team_child_direct.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child_agent_direct.flag">
                    <el-checkbox true-label="1" false-label="0">直推代理商满
                        <el-input v-model="team_child_agent_direct.amount" size="mini" v-enter-number
                                  style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child_agent_all.flag">
                    <el-checkbox true-label="1" false-label="0">团队代理商满
                        <el-input v-model="team_child_agent_all.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>

        <tr bgcolor="#FFFFFF">
            <td>
                <div v-for="(vo,index) in team_child_agent">
                    <el-checkbox-group v-model="vo.flag">
                        <el-checkbox true-label="1" false-label="0">团队-{{vo.name}}满
                            <el-input v-model="vo.amount" size="mini" v-enter-number style="width: 160px"
                                      @input="inputChange">
                                <template slot="append">人</template>
                            </el-input>
                        </el-checkbox>
                    </el-checkbox-group>
                </div>
            </td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td>
                <el-checkbox-group v-model="team_child_line.flag">
                    <el-checkbox true-label="1" false-label="0">
                        <el-input v-model="team_child_line.num" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                        </el-input>
                        条线各产生
                        <select v-model="team_child_line.level"
                                style="width: 100px;border-radius: 5px;border: #b4bccc 1px solid;"
                                class="forminput inputstyle textbox-text validatebox-text" placeholder="请选择">
                            <option v-for="item in levels"
                                    :key="item.level"
                                    :label="item.name"
                                    :value="item.level">
                            </option>
                        </select>
                        <el-input v-model="team_child_line.amount" size="mini" v-enter-number style="width: 160px"
                                  @input="inputChange">
                            <template slot="append">人</template>
                        </el-input>
                    </el-checkbox>
                </el-checkbox-group>
            </td>
        </tr>
    </table>
</form>

<script>
    var agentlevelVm = new Vue({
        el: '#<?php echo $uniqid;?>',
        data: {
            params: JSON.parse('<?php echo addslashes($dataset);?>'),
            levels: JSON.parse('<?php echo addslashes($levels);?>'),
            team_consume: {flag: 0, amount: ''},
            total_consume: {flag: 0, amount: ''},
            team_child: {flag: 0, amount: ''},
            team_child_direct: {flag: 0, amount: ''},
            team_child_agent_all: {flag: 0, amount: ''},
            team_child_agent_direct: {flag: 0, amount: ''},
            team_child_agent: [],
            team_child_line: {flag: 0,num: '',level: '',amount: ''}
        },
        created() {
            this.team_consume = this.params.upgrade_rules.team_consume ? this.params.upgrade_rules.team_consume : {flag: 0, amount: ''};
            this.total_consume = this.params.upgrade_rules.total_consume ? this.params.upgrade_rules.total_consume : {flag: 0, amount: ''};
            this.team_child = this.params.upgrade_rules.team_child ? this.params.upgrade_rules.team_child : {flag: 0, amount: ''};
            this.team_child_direct = this.params.upgrade_rules.team_child_direct ? this.params.upgrade_rules.team_child_direct : {flag: 0, amount: ''};
            this.team_child_agent_all = this.params.upgrade_rules.team_child_agent_all ? this.params.upgrade_rules.team_child_agent_all : {flag: 0, amount: ''};
            this.team_child_agent_direct = this.params.upgrade_rules.team_child_agent_direct ? this.params.upgrade_rules.team_child_agent_direct : {flag: 0, amount: ''};
            this.team_child_line = this.params.upgrade_rules.team_child_line ? this.params.upgrade_rules.team_child_line : {flag: 0,num: '',level: '',amount: ''};
            if(this.params.upgrade_rules.team_child_agent){
                this.team_child_agent = this.params.upgrade_rules.team_child_agent;
            }else {
                this.levels.forEach((it) => {
                    this.team_child_agent.push({
                        flag: 0,
                        name: it.name,
                        level: it.level,
                        amount: '',
                    });
                });
            }
        },
        methods: {
            inputChange() {
                this.$forceUpdate();
            },
            checkRules() {
                if (this.team_consume.flag==="1") {
                    agentlevelVm.params.upgrade_rules.team_consume = this.team_consume;
                }else{
                    delete agentlevelVm.params.upgrade_rules.team_consume;
                }
                if (this.total_consume.flag==="1") {
                    agentlevelVm.params.upgrade_rules.total_consume = this.total_consume;
                }else{
                    delete agentlevelVm.params.upgrade_rules.total_consume;
                }
                if (this.team_child.flag==="1") {
                    agentlevelVm.params.upgrade_rules.team_child = this.team_child;
                }else{
                    delete agentlevelVm.params.upgrade_rules.team_child;
                }
                if (this.team_child_direct.flag==="1") {
                    agentlevelVm.params.upgrade_rules.team_child_direct = this.team_child_direct;
                }else{
                    delete agentlevelVm.params.upgrade_rules.team_child_direct;
                }
                if (this.team_child_agent_all.flag==="1") {
                    agentlevelVm.params.upgrade_rules.team_child_agent_all = this.team_child_agent_all;
                }else{
                    delete agentlevelVm.params.upgrade_rules.team_child_agent_all;
                }
                if (this.team_child_agent_direct.flag==="1") {
                    agentlevelVm.params.upgrade_rules.team_child_agent_direct = this.team_child_agent_direct;
                }else{
                    delete agentlevelVm.params.upgrade_rules.team_child_agent_direct;
                }
                let team_child_agent_arr = [];
                this.team_child_agent.forEach((it)=>{
                    team_child_agent_arr.push(it);
                })
                if(team_child_agent_arr.length>0) {
                    agentlevelVm.params.upgrade_rules.team_child_agent = team_child_agent_arr;
                }else{
                    delete agentlevelVm.params.upgrade_rules.team_child_agent;
                }
                if (this.team_child_line.flag==="1") {
                    agentlevelVm.params.upgrade_rules.team_child_line = this.team_child_line;
                }else{
                    delete agentlevelVm.params.upgrade_rules.team_child_line;
                }
            },
            submit: function () {
                this.checkRules();
                let act = this.params.level ? 'edit' : 'add';
                $.post('/admin/agentlevel/' + act, agentlevelVm.params, function (data) {
                    if (data.ret === 0) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        $('#<?php echo $uniqid;?>').form('clear');
                        $("#agentlevelDlg").dialog('close');
                        agentlevelReload();
                    } else {
                        $.messager.alert('提示', data.msg, 'warning');
                    }
                });
            }
        }
    });
    $('#agentlevelsubmit').click(function (data) {
        agentlevelVm.submit();
    })
</script>
